package com.neuedu.dao.impl;

import com.neuedu.dao.ChongWuDao;
import com.neuedu.model.ChongWu;
import com.neuedu.model.XiaoQu;
import com.neuedu.utils.DbUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐丹
 * @date 2024/9/12
 */
public class ChongWuDaoImpl implements ChongWuDao {

    @Override
    public List<ChongWu> selectPage(String minDate, String maxDate, String likeName, Integer limitParam1, Integer limitParam2) {
        String where=" 1=1 ";
        List<Object> params=new ArrayList<>();
        if (minDate != null && !minDate.equals("")){
            where=where+"and chuangjianshijian >= ?";
            params.add(minDate);
        }
        if (maxDate != null && !maxDate.equals("")){
            where=where+"and chuangjianshijian <= ?";
            params.add(maxDate);
        }
        if (likeName != null && !likeName.equals("")){
            where=where+"and chongwumingcheng like ?";
            params.add("%"+likeName+"%");
        }

        where=where+"order by id desc limit ?,?";
        params.add(limitParam1);
        params.add(limitParam2);

        List<ChongWu> result = DbUtil.select(ChongWu.class, where, params.toArray());

        return result;
    }

    @Override
    public int count(String minDate, String maxDate, String likeName) {
        String where=" 1=1 ";
        List<Object> params=new ArrayList<>();
        if (minDate != null && !minDate.equals("")){
            where=where+"and chuangjianshijian >= ?";
            params.add(minDate);
        }
        if (maxDate != null && !maxDate.equals("")){
            where=where+"and chuangjianshijian <= ?";
            params.add(maxDate);
        }
        if (likeName != null && !likeName.equals("")){
            where=where+"and chongwumingcheng like ?";
            params.add("%"+likeName+"%");
        }
        List<ChongWu> result = DbUtil.select(ChongWu.class, where, params.toArray());

        return result==null?0:result.size();
    }

    @Override
    public int insertChongWu(ChongWu chongWu) {
        return DbUtil.insertSelective(chongWu);
    }

    @Override
    public int deleteChongWu(Integer[] ids) {
        String where="id in (";
        for (int i = 0; i <ids.length ; i++) {
            where=where+"?";
            if (i!=ids.length-1){
                where=where+",";
            }else {
                where=where+")";
            }
        }
        return DbUtil.delete(ChongWu.class,where,ids);
    }

    @Override
    public int updateChongWu(ChongWu chongWu) {
        return DbUtil.update(chongWu,"id=?",chongWu.getId());
    }

    @Override
    public ChongWu selectChongWuById(Integer id) {
        List<ChongWu> list = DbUtil.select(ChongWu.class, "id=?", id);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
